package com.cadre.web.controller.officer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cadre.common.exception.CadreException;
import com.cadre.common.utils.R;
import com.cadre.common.utils.oldutil.PoiUtil;
import com.cadre.common.utils.oldutil.ReportConfig;
import com.cadre.common.utils.oldutil.ResultEnum;
import com.cadre.system.domain.officer.AssemData;
import com.cadre.system.domain.officer.StatisCondition;
import com.cadre.system.domain.officer.StatismenuEntity;
import com.cadre.system.service.StatismenuService;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Api(value = "常用统计接口", tags = {"常用统计接口"})
@RequestMapping({"/profess/statismenu"})
@RestController
/* loaded from: StatismenuController.class */
public class StatismenuController {
    @Autowired
    private StatismenuService statismenuService;
    @Autowired
    private ReportConfig config;

    @PostMapping({"/listFromSetting"})
    @ApiOperation("根据行列指标设置，自定义统计的行和列")
    public R listFromSetting(@RequestParam Map<String, Object> map) {
        return this.statismenuService.listFromSetting(map);
    }

    @PostMapping({"/listFromQurey"})
    @ApiOperation("统计指标表格反查列表")
    public R listFromQurey(@RequestParam Map<String, Object> map) {
        return this.statismenuService.listFromQurey(map);
    }

    @GetMapping({"/list"})
    @ApiOperation("获取常用统计目录列表")
    public R list() {
        return R.ok().put("list", (Object) this.statismenuService.getBaseMapper().selectList(new LambdaQueryWrapper<>()));
    }

    @PostMapping({"/query"})
    @ApiOperation("综合统计分析")
    public R statisticalAnalysis(@RequestParam Map<String, Object> map) {
        return R.ok().put("list", (Object) this.statismenuService.queryAverage(map));
    }

    @GetMapping({"/queryDictByFcode"})
    @ApiOperation("根据父code获取统计分析字典项")
    public R getStatisDictByFcode(String fcode) {
        return R.ok().put("list", (Object) this.statismenuService.queryByFId(fcode));
    }

    @GetMapping({"/queryDict"})
    @ApiOperation("获取全部统计分析字典项")
    public R getStatisDict() {
        return R.ok().put("list", (Object) this.statismenuService.queryAllDic());
    }

    @PostMapping({"/updateCondition"})
    @ApiOperation("编辑统计条件")
    public R updateCondition(@RequestBody List<StatisCondition> list) {
        this.statismenuService.updateStatisCondition(list);
        return R.ok();
    }

    @GetMapping({"/deleteCondition"})
    @ApiOperation("删除统计分析")
    public R deleteStatisCondition(String conditionName) {
        this.statismenuService.deleteStatisCondition(conditionName);
        return R.ok();
    }

    @GetMapping({"/getConditionName"})
    @ApiOperation("获取用户已保存条件名称")
    public R getConditionName() {
        return R.ok().put("data", (Object) this.statismenuService.getConditionName());
    }

    @GetMapping({"/getCondition"})
    @ApiOperation("根据统计条件名称获取统计条件")
    public R getCondition(String name) {
        return R.ok().put("data", (Object) this.statismenuService.getCondition(name));
    }

    @PostMapping({"/inverseQueryA01"})
    @ApiOperation("综合统计分析数据反查-【民族，年龄，职务层次，性别】项")
    public R inverseQueryForA01(@RequestParam Map<String, Object> map) {
        return R.ok().put("data", (Object) this.statismenuService.inverseQueryForA01(map));
    }

    @PostMapping({"/inverseQueryA22"})
    @ApiOperation("综合统计分析数据反查-【党派】项")
    public R inverseQueryForA22(@RequestParam Map<String, Object> map) {
        return R.ok().put("data", (Object) this.statismenuService.inverseQueryForA22(map));
    }

    @PostMapping({"/inverseQueryA04Zg"})
    @ApiOperation("综合统计分析数据反查-【最高学历】项")
    public R inverseQueryForA04Zg(@RequestParam Map<String, Object> map) {
        return R.ok().put("data", (Object) this.statismenuService.inverseQueryForA04Zg(map));
    }

    @PostMapping({"/inverseQueryA04Qrz"})
    @ApiOperation("综合统计分析数据反查-【全日制学历】项")
    public R inverseQueryForA04Qrz(@RequestParam Map<String, Object> map) {
        return R.ok().put("data", (Object) this.statismenuService.inverseQueryForA04Qrz(map));
    }

    @PostMapping({"/exportExcel"})
    @ResponseBody
    public void exportExcel(@RequestParam Map<String, Object> map, HttpServletResponse response) {
        try {
            PoiUtil.writeExcel(setData(this.statismenuService.queryAverage(map)), response, "综合统计分析");
        } catch (IOException e) {
            e.printStackTrace();
            throw new CadreException(ResultEnum.EXCEL_ERROR);
        }
    }

    private Workbook setData(List<AssemData> data2) {
        List<List<Object>> li = formatData(data2);
        Workbook wb = PoiUtil.createWorkbook(this.config.getExcelStatisTmp());
        Sheet sheet = wb.getSheetAt(0);
        sheet.getRow(0);
        if (data2.size() > 1) {
            int n = data2.size() - 1;
            for (int i = 0; i < n; i++) {
                PoiUtil.copyRowsForCadrList(4, 4, i + 4, sheet);
            }
        }
        int n2 = li.size();
        for (int i2 = 0; i2 < n2; i2++) {
            Row row = sheet.getRow(i2 + 3);
            for (int j = 0; j < li.get(i2).size(); j++) {
                setCellValue(row.getCell(j), li.get(i2).get(j));
            }
        }
        return wb;
    }

    private List<List<Object>> formatData(List<AssemData> dataList) {
        List<List<Object>> arrayList = new ArrayList<>();
        for (AssemData vo : dataList) {
            List<Object> datList = Lists.newArrayList();
            datList.add(vo.getAvgAgeName());
            datList.add(vo.getAvgAgeValue());
            datList.add(vo.getAgeGroupName());
            datList.add(vo.getAgeGroupValue());
            datList.add(vo.getSexGroupName());
            datList.add(vo.getSexGroupValue());
            datList.add(vo.getMinzuName());
            datList.add(vo.getMinzuValue());
            datList.add(vo.getZzmianmaoName());
            datList.add(vo.getZzmianmaoValue());
            datList.add(vo.getZgxueliName());
            datList.add(vo.getZgxueliValue());
            datList.add(vo.getQrzxueliName());
            datList.add(vo.getQrzxueliValue());
            datList.add(vo.getZwccName());
            datList.add(vo.getZwccValue());
            arrayList.add(datList);
        }
        return arrayList;
    }

    private void setCellValue(Cell cell, Object obj) {
        if (obj instanceof Long) {
            cell.setCellValue(new BigDecimal(obj.toString().trim()).doubleValue());
        } else {
            cell.setCellValue(obj == null ? "" : obj.toString());
        }
    }

    @RequestMapping({"/info/{flowid}"})
//    @RequiresPermissions({"profess:statismenu:info"})
    public R info(@PathVariable("flowid") String flowid) {
        return R.ok().put("statismenu", (Object) ((StatismenuEntity) this.statismenuService.getById(flowid)));
    }

    @RequestMapping({"/save"})
//    @RequiresPermissions({"profess:statismenu:save"})
    public R save(@RequestBody StatismenuEntity statismenu) {
        this.statismenuService.save(statismenu);
        return R.ok();
    }

    @RequestMapping({"/update"})
//    @RequiresPermissions({"profess:statismenu:update"})
    public R update(@RequestBody StatismenuEntity statismenu) {
        this.statismenuService.updateById(statismenu);
        return R.ok();
    }

    @RequestMapping({"/delete"})
//    @RequiresPermissions({"profess:statismenu:delete"})
    public R delete(@RequestBody String[] flowids) {
        this.statismenuService.removeBatchByIds(Arrays.asList(flowids));
        return R.ok();
    }
}
